<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minEatingSpeed = function (piles, h) {
      // let total = piles.reduce((a, b) => a + b, 0);
      // let l = Math.ceil(total / h);
      let l = 1;
      let r = Math.max(...piles);
      while (l < r) {
        let m = l + ((r - l) >> 1);
        let curHours = piles.reduce((prev, cur) => prev + Math.ceil(cur / m), 0);
        if (curHours > h) {
          l = m + 1;
        } else if (curHours <= h) {
          r = m;
        }
      }
      return l;

    };
    console.log(minEatingSpeed(piles = [3, 6, 7, 11], h = 8))
  </script>
</body>

</html>